package com.java2404.micro.merchant.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.java2404.micro.common.entity.Goods;
import com.java2404.micro.common.entity.Merchant;
import com.java2404.micro.common.entity.PageBean;
import com.java2404.micro.common.resp.Result;
import com.java2404.micro.common.utils.MD5;
import com.java2404.micro.merchant.mapper.GoodsMapper;
import com.java2404.micro.merchant.mapper.MerchantMapper;
import com.java2404.micro.merchant.service.MerchantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 商家业务实现类
 * @author HP
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public Result login(Merchant merchant) {
        // 使用 LambdaQueryWrapper 根据 mname 查找商户
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Merchant::getMname, merchant.getMname());

        // 使用 MyBatis-Plus 的 selectOne 查询商户
        Merchant loginMerchant = merchantMapper.selectOne(queryWrapper);

        //账号判断
        if (loginMerchant == null) {
            //账号错误
            return Result.error("Mname is error");
        }

        //判断密码
        if (!StringUtils.isEmpty(merchant.getMpasswd())
                && loginMerchant.getMpasswd().equals(MD5.getMD5Checksum(merchant.getMpasswd())))
        {

            return Result.success(loginMerchant);
        }

        //密码错误
        return Result.error("password is error");
    }

    @Override
    public Result regist(Merchant merchant) {
        // 检查账号名是否为空
        if (merchant.getMname() == null || merchant.getMname().length() == 0) {
            return Result.error("请填写账号");
        }

        // 使用 MyBatis-Plus 查询是否已有相同账号
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Merchant::getMname, merchant.getMname());
        Merchant existingMerchant = merchantMapper.selectOne(queryWrapper);

        // 如果账号已经存在，返回错误提示
        if (existingMerchant != null) {
            return Result.error("该账号已被注册");
        }

        // 如果账号不存在，进行注册
        merchant.setMpasswd(MD5.getMD5Checksum(merchant.getMpasswd()));  // 密码加密
        merchant.setCreate_time(LocalDateTime.now());  // 设置创建时间
        merchantMapper.insert(merchant);  // 插入数据

        return Result.success("注册成功");
    }

    @Override
    public PageBean<Merchant> ShowMerchant(Integer pageNum, Integer pageSize) {
        PageBean<Merchant> pb=new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        List<Merchant> as = merchantMapper.selectList(queryWrapper);
        Page<Merchant> p=(Page<Merchant>) as;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void updateMerchant(Merchant merchant) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid", merchant.getMid());  // 根据商户ID更新记录
        merchantMapper.update(merchant,queryWrapper);
    }

    @Override
    public void audit(int mid, int mstatus) {
        Merchant merchant = new Merchant();
        merchant.setMid(mid);
        merchant.setMstatus(mstatus);
        }

    @Override
    public Merchant getById(int mid) {
        return merchantMapper.selectById(mid);
    }
}
